<?php  
class ControllerModuleBlog extends Controller {
	protected function index($setting) {

		$this->language->load('module/blog');
		
		$this->data['widgetId'] = $setting['widget_id'];
		
		switch((int)$setting['widget_id']){
			case 1:
				
				if (isset($this->request->get['path'])) {
					$parts = explode('_', (string)$this->request->get['path']);
				} else {
					$parts = array();
				}
				
				if (isset($parts[0])) {
					$this->data['category_id'] = $parts[0];
				} else {
					$this->data['category_id'] = 0;
				}
				
				if (isset($parts[1])) {
					$this->data['child_id'] = $parts[1];
				} else {
					$this->data['child_id'] = 0;
				}
											
				$this->load->model('blog/category');
				$this->load->model('blog/post');
				
				$this->data['categories'] = array();
							
				$categories = $this->model_blog_category->getCategories(0);
				//echo '<pre>';
				//print_r($categories);
				//echo '</pre>';
				//exit;
				
				foreach ($categories as $category) {
					$children_data = array();
					
					$children = $this->model_blog_category->getCategories($category['id']);
					
					foreach ($children as $child) {
						$data = array(
							'filter_category_id'  => $child['id'],
							'filter_sub_category' => true
						);		
							
						if ($setting['count']) {
							$post_total = $this->model_blog_post->rgetTotalPosts($data);
							
							$children_data[] = array(
								'category_id' => $child['id'],
								'name'        => $child['name'] . ' (' . $post_total . ')',
								'href'        => $this->url->link('blog/post/category', 'path=' . $category['id'] . '_' . $child['id'])	
							);						
						} else {
							$children_data[] = array(
								'category_id' => $child['id'],
								'name'        => $child['name'],
								'href'        => $this->url->link('blog/post/category', 'path=' . $category['id'] . '_' . $child['id'])	
							);						
						}			
					}
			
					$data = array(
						'filter_category_id'  => $category['id'],
						'filter_sub_category' => true	
					);		
					
					if ($setting['count']) {
						$post_total = $this->model_blog_post->rgetTotalPosts($data);
					
						$this->data['categories'][] = array(
							'category_id' => $category['id'],
							'name'        => $category['name'] . ' (' . $post_total . ')',
							'children'    => $children_data,
							'href'        => $this->url->link('blog/post/category', 'path=' . $category['id'])
						);				
					} else {
						$this->data['categories'][] = array(
							'category_id' => $category['id'],
							'name'        => $category['name'],
							'children'    => $children_data,
							'href'        => $this->url->link('blog/post/category', 'path=' . $category['id'])
						);			
					}
				}
			break;
			case 2:
				$this->load->model('blog/roll');
				
				$this->data['favouriate_links'] = array();
							
				$this->data['favouriate_links'] = $this->model_blog_roll->getFavouriateLinks();
			break;
			case 3:		
				if (isset($this->request->get['apath'])) {
					$parts = explode('_', (string)$this->request->get['apath']);
				} else {
					$parts = array();
				}
				
				if (isset($parts[0])) {
					$this->data['year'] = $parts[0];
				} else {
					$this->data['year'] = 0;
				}
				
				if (isset($parts[1])) {
					$this->data['month'] = $parts[1];
				} else {
					$this->data['month'] = 0;
				}
											
				$this->load->model('blog/post');
				
				$this->data['archieves'] = array();
							
				$years = $this->model_blog_post->getYears();
				
				foreach ($years as $year) {
					$months_data = array();
					
					$months = $this->model_blog_post->getMonths($year['id']);
					
					foreach ($months as $month) {
						if ($setting['count']) {
							$months_data[] = array(
								'archieve_id' => $month['id'],
								'name'        => $month['month'] . ' (' . $month['cnt'] . ')',
								'href'        => $this->url->link('blog/post/archieve', 'apath=' . $year['id'] . '_' . $month['id'])	
							);						
						} else {
							$months_data[] = array(
								'archieve_id' => $month['id'],
								'name'        => $month['month'],
								'href'        => $this->url->link('blog/post/archieve', 'apath=' . $year['id'] . '_' . $month['id'])	
							);						
						}			
					}

					if ($setting['count']) {
						$this->data['archieves'][] = array(
							'archieve_id' => $year['id'],
							'name'        => $year['year'] . ' (' . $year['cnt'] . ')',
							'children'    => $months_data,
							'href'        => $this->url->link('blog/post/archieve', 'apath=' . $year['year'])
						);				
					} else {
						$this->data['archieves'][] = array(
							'archieve_id' => $year['id'],
							'name'        => $year['year'],
							'children'    => $months_data,
							'href'        => $this->url->link('blog/post/archieve', 'apath=' . $year['year'])
						);			
					}
				}
			break;
			case 4:
				$this->data['facebook_link'] = $this->config->get('config_facebook_link');
				$this->data['twitter_link'] = $this->config->get('config_twitter_link');
			break;
			case 5:	
				$this->data['want2sell_link'] = $this->config->get('config_want2sell_link');
			break;
			case 6:	
				$this->data['ourartist_link'] = $this->config->get('config_ourartist_link');
			break;
		}
		
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/blog.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/module/blog.tpl';
		} else {
			$this->template = 'default/template/module/blog.tpl';
		}
		
		$this->render();
  	}
}
?>